#include "cn_data_flash.h"
#include "cn_crc.h"

static int read_para(u8 *data,u32 len);
static int write_para(u8 *data,u32 len);

static char sta_ssid[APSTA_SSID_SIZE]   = "CN_TECH";
static char sta_pwd[APSTA_PWD_SIZE]     = "123456789";
static char apssid[APSTA_SSID_SIZE]     = "wifi2usart";
static char appwd[APSTA_PWD_SIZE]       = "123456789";
static char dev_name[DEV_NAME_SIZE]     = "cn_dev";
static u8 dummy[500];
static cn_network_para  tmp_para = {0};

static int read_para(u8 *data,u32 len)
{
    int err = 0;
    err = tls_fls_read(PARA_DATA_ADDR, data, len);
    wm_printf("tls_fls_read: %d len:%d\r\n",err,len);
    for (u32 i = 0; i < len; i++)
    {
        wm_printf("%02x ",data[i]);
    }
    wm_printf("\r\n");
    
    if(ChkCrcValueEx(data,len - 1,0xffff))
    {
        err = -1;
    }
    else
    {
        if(data[len - 1] == 0xe7)
        {
            err = 0;
        }
        else
        {
            err = -1;
        }    
    }
    wm_printf("tls_fls_read crc: %d\r\n",err);
    return err; 
}

static int write_para(u8 *data,u32 len)
{
    int err = 0;
    u16 crc;
    crc = ChkCrcValueEx(data,len- 3,0xffff);
	*(uint16_t*)(data + len - 3) = crc;
    *(data+len - 1) = 0xe7;	
    err = tls_fls_write(PARA_DATA_ADDR, data, len);
#if 0
    wm_printf("tls_fls_write: %d len:%d crc:%d\r\n",err,len,crc);
    for (u32 i = 0; i < len; i++)
    {
        wm_printf("%02x ",data[i]);
    }
    wm_printf("\r\n");
#endif
    return err; 
}

int network_para_init(void)
{
    int err = 0;
    u16 crc = 0;					
    err = read_para((u8 *)&tmp_para, sizeof(cn_network_para));
    if(err < 0) //校验失败
    {
        wm_printf("network_para_init fail %d \r\n",err);

        tmp_para.network_type = CLIENT_TYPE;

        tmp_para.ap_ip_addr  = ipaddr_addr("192.168.8.2");
        tmp_para.ap_gw_addr  = ipaddr_addr("192.168.8.1");
        tmp_para.ap_msk_addr = ipaddr_addr("255.255.255.0");

        tmp_para.target_ip   = ipaddr_addr("192.168.8.5");
        tmp_para.target_port = 8001;
        tmp_para.local_port  = 7001;

        memset(tmp_para.sta_ssid,0,APSTA_SSID_SIZE);
        strcpy(tmp_para.sta_ssid,sta_ssid);

        memset(tmp_para.sta_pwd,0,APSTA_PWD_SIZE);
        strcpy(tmp_para.sta_pwd,sta_pwd);  

        memset(tmp_para.ap_ssid,0,APSTA_SSID_SIZE);
        strcpy(tmp_para.ap_ssid,apssid);

        memset(tmp_para.ap_pwd,0,APSTA_PWD_SIZE);
        strcpy(tmp_para.ap_pwd,appwd);            

        memset(tmp_para.dev_name,0,DEV_NAME_SIZE);
        strcpy(tmp_para.dev_name,dev_name); 

        err = write_para((u8 *)&tmp_para, sizeof(cn_network_para));
        read_para((u8 *)dummy, sizeof(cn_network_para));
    }
    else
    {
        wm_printf("network_para_init ok %d \r\n",err);
    }
    return err;
}

int get_ap_message(u32 *ip_addr,u32 *gw_addr,u32 *msk_addr)
{
    *ip_addr  = tmp_para.ap_ip_addr;
    *gw_addr  = tmp_para.ap_gw_addr;
    *msk_addr = tmp_para.ap_msk_addr;
    return 0;
}

int set_ap_message(u32 ip_addr,u32 gw_addr,u32 msk_addr)
{
    u8 flag = 0,err = 0;
    if (ip_addr != tmp_para.ap_ip_addr)
    {
        flag = 1;
        tmp_para.ap_ip_addr = ip_addr;
    }
    if (gw_addr != tmp_para.ap_gw_addr)
    {
        flag = 1;
        tmp_para.ap_gw_addr = gw_addr;
    }
    if (msk_addr != tmp_para.ap_msk_addr)
    {
        flag = 1;
        tmp_para.ap_msk_addr = msk_addr;
    }   
    if(flag)
    {
        err = write_para((u8 *)&tmp_para, sizeof(cn_network_para));
    }
    return err;
}

int get_net_message(u8 *type,u32 *target_ip,u32 *target_port,u32 *local_port)
{
    *type        = tmp_para.network_type;
    *target_ip   = tmp_para.target_ip;
    *target_port = tmp_para.target_port;
    *local_port  = tmp_para.local_port;
    return 0;
}

int set_net_message(u8 type,u32 target_ip,u32 target_port,u32 local_port)
{
    u8 flag = 0,err = 0;
    if (type != tmp_para.network_type)
    {
        flag = 1;
        tmp_para.network_type = type;
    }
    
    if (target_ip != tmp_para.target_ip)
    {
        flag = 1;
        tmp_para.target_ip = target_ip;
    }
    if (target_port != tmp_para.target_port)
    {
        flag = 1;
        tmp_para.target_port = target_port;
    }
    if (local_port != tmp_para.local_port)
    {
        flag = 1;
        tmp_para.local_port = local_port;
    }   
    if(flag)
    {
        err = write_para((u8 *)&tmp_para, sizeof(cn_network_para));
    }
    return err;
}

int get_sta_wifi(char *ssid,char *pwd)
{
    strcpy(ssid,tmp_para.sta_ssid);
    strcpy(pwd,tmp_para.sta_pwd);     
    return 0;   
}

int set_sta_wifi(char *ssid,char *pwd)
{
    u8 flag = 0,err = 0;
    if (strcmp(ssid,tmp_para.sta_ssid) != 0)
    {
        flag = 1;
        printf("set_sta_wifi1 \n");
        memset(tmp_para.sta_ssid,0,APSTA_SSID_SIZE);
        strcpy(tmp_para.sta_ssid,ssid);
    }
    if (strcmp(pwd,tmp_para.sta_pwd) != 0)
    {
        flag = 1;
        printf("set_sta_wifi2 \n");
        memset(tmp_para.sta_pwd,0,APSTA_PWD_SIZE);
        strcpy(tmp_para.sta_pwd,pwd);
    } 
    if(flag)
    {
        err = write_para((u8 *)&tmp_para, sizeof(cn_network_para));
    }
    return err;   
}

int get_ap_wifi(char *ssid,char *pwd)
{
    strcpy(ssid,tmp_para.ap_ssid);
    strcpy(pwd,tmp_para.ap_pwd);     
    return 0;   
}

int set_ap_wifi(char *ssid,char *pwd)
{
    u8 flag = 0,err = 0;
    if (strcmp(ssid,tmp_para.ap_ssid) != 0)
    {
        flag = 1;
        printf("set_ap_wifi1 \n");
        memset(tmp_para.ap_ssid,0,APSTA_SSID_SIZE);
        strcpy(tmp_para.ap_ssid,ssid);
    }
    if (strcmp(pwd,tmp_para.ap_pwd) != 0)
    {
        flag = 1;
        printf("set_ap_wifi2 \n");
        memset(tmp_para.ap_pwd,0,APSTA_PWD_SIZE);
        strcpy(tmp_para.ap_pwd,pwd);
    } 
    if(flag)
    {
        err = write_para((u8 *)&tmp_para, sizeof(cn_network_para));
    }
    return err;   
}

int get_dev_name(char *name)
{
    printf("get_dev_name1 %s\n",tmp_para.dev_name);
    strcpy(name,tmp_para.dev_name);  
    printf("get_dev_name2 %s\n",name);   
    return 0;      
}

int set_dev_name(char *name)
{
    u8 flag = 0,err = 0;
    if (strcmp(name,tmp_para.dev_name) != 0)
    {
        flag = 1;
        printf("set_dev_name \n");
        memset(tmp_para.dev_name,0,APSTA_SSID_SIZE);
        strcpy(tmp_para.dev_name,name);
    }
    if(flag)
    {
        err = write_para((u8 *)&tmp_para, sizeof(cn_network_para));
    }
    return err;    
}

